// pages/vote/vote.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        vote: {},
        username: '',
        reqData: {},
        multiple: 0,
        options: [],
        optionsValue: [],
        title: '',
        date: '',
        time: '',
        desc: '',
        isAnoymous: 0,
        url: 'http://192.168.31.34:8081'
        // url: 'http://192.168.1.69:8081'
        // url:'http://172.20.10.4:8081'
    },
    handleChange(e) {
        this.setData({ multiple: e.detail.value })
    },
    handleTitle(e) {
        this.setData({ title: e.detail.value })
    },
    handleDesc(e) {
        this.setData({ desc: e.detail.value })
    },
    handleOption(e) {
        let index = e.currentTarget.dataset.index
        let optionsValue = []
        if (this.data.vote.ops) {
            this.data.vote.ops.forEach(op => {
                optionsValue.push(op.option)
            })
        } else {
            optionsValue = this.data.optionsValue;
        }
        if (optionsValue[index] || optionsValue[index] == '') {
            optionsValue.splice(index, 1, e.detail.value)
        } else {
            optionsValue.push(e.detail.value)
        }
        this.setData({ optionsValue })
    },
    
    handleSubmit() {
        const _this = this;
        if (this.data.vote.id) { // 修改操作
            wx.request({
                url: _this.data.url + '/vote/vote/edit1',
                method:"POST",
                data: {
                    id: _this.data.vote.id,
                    title: _this.data.title,
                    multiple: _this.data.multiple,
                    desc: _this.data.desc,
                    username: _this.data.username,
                    endDate: _this.data.date + " " + _this.data.time,
                    isAnoymous: _this.data.isAnoymous
                },
                success: function () {
                    // 封装选项对象
                    let ops = _this.data.vote.ops
                    let values = []
                    for (let i = 0; i < _this.data.optionsValue.length; i ++) {
                        values.push({
                            id: ops[i] ? ops[i].id : null,
                            voteId: _this.data.vote.id,
                            option: _this.data.optionsValue[i]
                        })
                    }
                    wx.request({
                        url: _this.data.url + '/vote/options/edit1',
                        method: 'POST',
                        data: JSON.stringify(values),
                        success: function (res) {
                            wx.switchTab({
                                url: '/pages/index/index',
                                success() {
                                    wx.showToast({
                                        title: '修改成功',
                                        icon: 'success',
                                    })
                                }
                            })
                        }
                    })
                }
            })
        } else {
            wx.request({
                url: _this.data.url + '/vote/vote/add1',
                method: 'POST',
                data: {
                    title: _this.data.title,
                    multiple: _this.data.multiple,
                    desc: _this.data.desc,
                    username: _this.data.username,
                    endDate: _this.data.date + " " + _this.data.time,
                    isAnoymous: _this.data.isAnoymous
                },
                success: function (res) {
                    if (res.data.code == 0) {
                        const id = res.data.data;
                        wx.request({
                            url: _this.data.url + '/vote/options/add1/' + id,
                            method: 'POST',
                            data: JSON.stringify(_this.data.optionsValue),
                            success: function (res) {
                                wx.redirectTo({
                                    url: '/pages/result/result?id=' + id,
                                    success() {
                                        wx.showToast({
                                            title: '新增成功',
                                            icon: 'success',
                                        })
                                    }
                                })
                            }
                        })
                    }
                }
            })
        }
    },
    handleAdd() {
        let options = this.data.options;
        options.push({ place: '选项' + (options.length + 1) });
        this.setData({ options })
    },
    handleSub(e) {
        const index = e.currentTarget.dataset.index;
        let options = this.data.options;
        options.splice(index, index + 1);
        this.setData({ options })
    },
    forMatterDate(date) {
        const year = date.getFullYear();
        const month = date.getMonth() + 1;
        const day = date.getDate();
        return year + '-' + month + '-' + day;
    },
    forMatterTime(date) {
        const hour = date.getHours();
        const minu = date.getMinutes();
        return hour + '\:' + minu;
    },
    dateChange(e) {
        this.setData({ date: e.detail.value })
    },
    timeChange(e) {
        this.setData({ time: e.detail.value })
    },
    switchChange(e) {
        this.setData({ isAnoymous: e.detail.value ? 0 : 1 })
    },
    onShow() {
        let options = [];
        if (this.data.vote.ops) {
            this.data.vote.ops.forEach(op =>
                options.push({ place: '', value: op.option })
            )
        } else {
            for (let i = 1; i < 3; i++) {
                options.push({ place: '选项' + i, value: '' })
            }
        }
        this.setData({
            options,
        })
        if (this.data.vote.endDate) {
            this.setData({
                date: this.forMatterDate(new Date(this.data.vote.endDate)),
                time: this.forMatterTime(new Date(this.data.vote.endDate)),
            })
        } else {
            this.setData({
                date: this.forMatterDate(new Date()),
                time: this.forMatterTime(new Date())
            })
        }


    },
    onLoad(options) {
        if (options) {
            if (options.vote) {
                this.setData({ vote: JSON.parse(options.vote) })
            }
        }
        const _this = this;
        wx.getStorage({
            key: 'username',
            success: function (res) {
                _this.setData({ username: res.data })
            }
        })
    },
})